package com.leetroll.common.enums;

import com.leetroll.common.global.BaseSysConf;
import com.leetroll.common.utils.JsonUtils;
import lombok.extern.slf4j.Slf4j;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * 行为枚举类
 */
@Slf4j
public enum EBehavior {
    /**
     * 用户行为
     */
    CLICK_LOGIN("点击登录", "click_login"),
    CLICK_LOGINOUT("点击退出", "click_loginout"),
    DELETE_ORDER("删除订单", "delete_order"),
    PAY_WX("微信支付", "pay_wx"),
    CLICK_SORT("点击模型分类", "click_sort"),
    CLICK_ITEM("点击模型", "click_item"),
    LEET_PRAISE("点赞", "leet_praise"),
    LEET_SEARCH("点击搜索", "leet_search"),
    LEET_VIDEO("点击模型视频", "study_video"),
    VISIT_PAGE("访问页面", "visit_page"),
    LEET_AUTHOR("点击作者", "leet_author"),
    PUBLISH_COMMENT("发表评论", "publish_comment"),
    DELETE_COMMENT("删除评论", "delete_comment"),
    REPORT_COMMENT("举报评论", "report_comment");

    private String content;
    private String behavior;

    EBehavior(String content, String behavior) {
        this.content = content;
        this.behavior = behavior;
    }

    /**
     * 根据value返回枚举类型，主要在switch中使用
     */
    public static EBehavior getByValue(String value) {
        for (EBehavior behavior : values()) {
            if (Objects.equals(behavior.getBehavior(), value)) {
                return behavior;
            }
        }
        return null;
    }

    public static Map<String, String> getModuleAndOtherData(EBehavior behavior,
                                                            Map<String, Object> nameAndArgsMap,
                                                            String bussinessName) {
        String otherData = "";
        String moduleId = "";
        String itemId = "";

        switch (behavior) {
            //登录|退出
            case CLICK_LOGIN:
            case CLICK_LOGINOUT: {
                if (nameAndArgsMap.get(BaseSysConf.PHONE) != null) {
                    otherData = nameAndArgsMap.get(BaseSysConf.PHONE).toString();
                }
                otherData = bussinessName;
            }
                break;
            case CLICK_SORT:
            case LEET_SEARCH: {
                // 判断是否点击模块分类
                if (nameAndArgsMap.get(BaseSysConf.BLOG_SORT_UID) != null) {
                    moduleId = nameAndArgsMap.get(BaseSysConf.BLOG_SORT_UID).toString();
                }
                otherData = bussinessName;
            }
                break;
            case CLICK_ITEM: {
                // 判断是否进行搜索
                if (nameAndArgsMap.get(BaseSysConf.KEYWORDS) != null) {
                    otherData = nameAndArgsMap.get(BaseSysConf.KEYWORDS).toString();
                }
            }
                break;
            // 点赞
            case LEET_PRAISE: {
                // 判断是否点赞
                if (nameAndArgsMap.get(BaseSysConf.UID) != null) {
                    moduleId = nameAndArgsMap.get(BaseSysConf.UID).toString();
                }
            }
                break;
            case VISIT_PAGE: {
                // 访问页面
                if (nameAndArgsMap.get(BaseSysConf.PAGE_NAME) != null) {
                    otherData = nameAndArgsMap.get(BaseSysConf.PAGE_NAME).toString();
                } else {
                    otherData = bussinessName;
                }
            }
                break;
            // 举报评论
            case PUBLISH_COMMENT:
            // 删除评论
            case DELETE_COMMENT:
            // 发表评论
            case REPORT_COMMENT: {
                Object object = nameAndArgsMap.get(BaseSysConf.COMMENT_VO);
                Map<String, Object> map = JsonUtils.objectToMap(object);
                assert map != null;
                if (map.get(BaseSysConf.CONTENT) != null) {
                    otherData = map.get(BaseSysConf.CONTENT).toString();
                }
            }
                break;
            case DELETE_ORDER:
                otherData = bussinessName;
                break;
            case PAY_WX:
                otherData = bussinessName;
                break;
            default: {
                log.info("其它行为");
            }
        }

        Map<String, String> result = new HashMap<>();
        result.put(BaseSysConf.MODULE_ID, moduleId);
        result.put(BaseSysConf.ITEM_ID, itemId);
        result.put(BaseSysConf.OTHER_DATA, otherData);
        return result;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public String getBehavior() {
        return behavior;
    }

    public void setBehavior(String behavior) {
        this.behavior = behavior;
    }
}